﻿using ConfExpert.Forms.Base;
using DevExpress.Utils;
using DevTeam;

using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo._7C
{
    public partial class Executions : XForm
    {
        int CKBatchId = 0, CikisPartisiId = 0;
        public Executions(int ckBatchId, int cikisPartisiId)
        {
            InitializeComponent();

            this.CKBatchId = ckBatchId;
            this.CikisPartisiId = cikisPartisiId;

            if (this.CikisPartisiId > 0)
                this.Text += ". CPId: " + this.CikisPartisiId.ToString();
        }

        BindingSource bs1, bs2;
        private void Executions_Load(object sender, EventArgs e)
        {
            this.

            btnHesapla.Click += new EventHandler(btnHesapla_Click);

            if (CikisPartisiId > 0)
            {
                panelControl2.Visible = false;
                hesapla();
            }

            gv1.DoubleClick += new EventHandler(gv1_DoubleClick);
            //gv1.RowCellClick += new DevExpress.XtraGrid.Views.Grid.RowCellClickEventHandler(gv1_RowCellClick);
            gv1.RowCellStyle += new DevExpress.XtraGrid.Views.Grid.RowCellStyleEventHandler(gv1_RowCellStyle);
        }

        string sel = "QtySecilen";
        void gv1_DoubleClick(object sender, EventArgs e)
        {
            switch (gv1.FocusedColumn.FieldName)
            {
                case "QtySecilen":
                case "QtyCikilan":
                    sel = gv1.FocusedColumn.FieldName;
                    break;
            }

            if (sel != string.Empty)
                if (gv1.DataTable().Columns["QtyFARK"].Expression != "ISNULL(" + sel + ",0)-ISNULL(Qty,0)")
                {
                    gv1.ChangeExpression("QtyFARK", "ISNULL(" + sel + ",0)-ISNULL(Qty,0)");
                    gv1.FormatAsNumber(1, "QtyAx", "QtyGx", "Qty", "QtySecilen", "QtyCikilan", "QtyFARK");
                    gv1.AddSummary("QtyAx", "QtyGx", "Qty", "QtySecilen", "QtyCikilan", "QtyFARK");
                }
        }

        void gv1_RowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
        {
            if (e.Column.FieldName == "Qty" ||
                e.Column.FieldName == sel ||
                e.Column.FieldName == "QtyFARK")
                e.Appearance.Font = new Font(AppearanceObject.DefaultFont, FontStyle.Bold);

            //if (e.Column.FieldName == "QtyFARK")
            //    e.Appearance.ForeColor = Color.Crimson;
        }

        void btnHesapla_Click(object sender, EventArgs e)
        {
            hesapla();
        }

        private void hesapla()
        {
            string lst = memoEdit1.Text.Trim();
            if (lst.Length > 1)
            {
                lst = lst.Replace("\t", string.Empty).Replace(" ", string.Empty).Replace(Environment.NewLine + Environment.NewLine, Environment.NewLine);
                if (lst.Substring(lst.Length - 1, 1) == Environment.NewLine)
                    lst = lst.Substring(0, lst.Length - 1);
                lst = lst.Replace(Environment.NewLine, ",");
            }

            DataSet ds = Session.DataManager.ExecuteDataSet("GetExecutions", CKBatchId, CikisPartisiId, lst);
            bs1 = gv1.Init(ds.Tables[0]);
            bs2 = gv2.Init(ds.Tables[1]);
            gv1.MakeReadOnly();

            if (sel != string.Empty)
                gv1.ChangeExpression("QtyFARK", "ISNULL(" + sel + ",0)-ISNULL(Qty,0)");
            gv1.FormatAsNumber(1, "QtyAx", "QtyGx", "Qty", "QtySecilen", "QtyCikilan", "QtyFARK");
            gv1.AddSummary("QtyAx", "QtyGx", "Qty", "QtySecilen", "QtyCikilan", "QtyFARK");

            gv2.AddSummary("Adet");

            gv1.BestFitColumns();
            gv2.BestFitColumns();
        }
    }
}